﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Automation;
using Compiled_Tests.AutomationAbstractions;
using Tedds_Tool.Automatables;
using Tedds_Tool;
using LEET.CommonInterfaces.Automation;
using LEET.LEETCommon.Model;


namespace LEET.LEETCommon.AutomationAbstractions
{
    public abstract class AutomationElementEventListenerBase : AutomationElementEventListener
    {
        public IObservableWidget RootTarget { get; protected set; }

        public virtual void Listen(IObservableWidget widget)
        {
            throw new NotImplementedException();
        }

        public virtual void Listen(IObservableWidget widget, String property, String newValue)
        {
            throw new NotImplementedException();
        }


        public virtual void Dispose()
        {
            Listening = false;
        }

        protected void CatchEvent(object sender, StructureChangedEventArgs e)
        {
            try
            {
                Listen(new ObservableWidget((AutomationElement)sender));
            }
            catch (NoValidIDException) { }
        }

        protected void CatchEvent(object sender, AutomationEventArgs e)
        {
            try
            {
                Listen(new ObservableWidget((AutomationElement)sender));
            }
            catch (NoValidIDException) { }
        }

        protected void CatchEvent(object sender, AutomationPropertyChangedEventArgs e)
        {
            try
            {
                AutomationPropertyChangedEventArgs ev = e as AutomationPropertyChangedEventArgs;
                Listen(new ObservableWidget((AutomationElement)sender), ev.Property.ProgrammaticName, ev.NewValue.ToString().Trim());
            }
            catch (NoValidIDException) { }
        }

        public event EventDelegate Handlers;

        protected void Delagate(Event_Pair evnt){
            Handlers(null, evnt);
        }

        public bool Listening { get; protected set; }
    }
}
